import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/event_or_delegate/reload_event.dart';
import 'package:flutter_app_unionad/model/task_item_model.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/ui/basice/my_button.dart';
import 'package:flutter_app_unionad/view_model/change_device_event.dart';

class TaskListItemWidget extends StatefulWidget {
  final TaskItemModel? itemModel;
  final Function()? onTap;

  const TaskListItemWidget({Key? key, this.itemModel, this.onTap}) : super(key: key);

  @override
  _TaskListItemWidgetState createState() => _TaskListItemWidgetState();
}

class _TaskListItemWidgetState extends State<TaskListItemWidget> {
  StreamSubscription? taskTimer;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //视频任务需要倒计时
    // print('-------------------------initState');

    taskTimer = eventBus.on<TaskDownTimerEvent>().listen((event) {
      // print('-------------------------${widget.itemModel?.taskName}');
      if (widget.itemModel!.taskOperation == 301 || widget.itemModel!.taskOperation == 302) {
        if (mounted) setState(() {});
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    // print('-------------------------dispose');

    taskTimer?.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return buildItemWidget(
      subTitle:
          "${widget.itemModel!.taskName}(已完成${widget.itemModel!.completeCount == null ? 0 : widget.itemModel!.completeCount}次)",
      img: widget.itemModel!.icon,
      itemModel: widget.itemModel,
      onTap: () {
        if (widget.onTap != null) widget.onTap?.call();
      },
    );
  }

  Widget buildItemWidget({String? subTitle, String? img, Function()? onTap, TaskItemModel? itemModel}) {
    String title = '去播放';
    bool isOnTap = true;
    Color color = MyColors.color_245_174_41;

    if (itemModel!.completeCount == null) itemModel.completeCount = 0;
    if (itemModel.completeCount! >= itemModel.needCompleteCount!) {
      color = MyColors.color_151_151_151;
      isOnTap = false;
    }
    switch (itemModel.taskOperation) {
      case 304:
        {
          title = '邀请好友';
          if (itemModel.userTaskStatus == 1) {
            //已完成
            title = '领取奖励';
            color = MyColors.color_226_53_25;
            isOnTap = true;
          }
        }

        break;
      case 308:
        {
          title = '去分享';
          if (itemModel.userTaskStatus == 1) {
            //已完成
            title = '领取奖励';
            color = MyColors.color_226_53_25;
            isOnTap = true;
          }
        }

        break;
      case 301:
      case 302:
        {
          if (itemModel.userTaskStatus == 1) {
            //已完成
            title = '领取奖励';
            color = MyColors.color_226_53_25;
            isOnTap = true;
          } else {
            // print('---------------------------------');
            // // print(itemModel.taskName);
            // print(itemModel.updateTime);
            // print(DateTime.now().millisecondsSinceEpoch);
            if (itemModel.dateCap == null) {
              itemModel.dateCap = 0;
            }
            if (itemModel.updateTime == null) {
              itemModel.updateTime = 0;
            }
            int updateTime =
                itemModel.dateCap! * 60 - ((DateTime.now().millisecondsSinceEpoch - itemModel.updateTime!) ~/ 1000);
            if (updateTime > 0) {
              //需要显示倒计时
              color = MyColors.color_151_151_151;
              isOnTap = false;
              ///秒 转化为分钟
              title = secondToTime(updateTime);
            }
          }
          break;
        }
    }
    return Row(
      children: [
        ShowPicWidget(
          img ?? '',
          width: 24.px,
          height: 24.px,
          radius: 0,
        ),
        MySizeBox(width: 10),
        Expanded(
          child: Text(
            subTitle ?? '',
            style: TextStyle(
              color: MyColors.color_27_27_27,
              fontSize: MyFont.font12,
              fontWeight: MyFontWeight.regular,
            ),
          ),
        ),
        MySizeBox(width: 5),
        MyButton(
          title: title,
          width: 84.px,
          height: 30.px,
          color: color,
          titleStyle: TextStyle(
            color: Colors.white,
            fontSize: MyFont.font12,
            fontWeight: MyFontWeight.bold,
          ),
          onTap: () {
            print('任务被点击 ');

            if (!isOnTap) {
              return;
            }
            if (onTap != null) {
              onTap();
            }
          },
        ),
      ],
    );
  }

  String secondToTime(int second) {
    if (second < 60) {
      int s = second;

      return (s < 10 ? '0$s' : '$s') + 's';
    } else if (second < 60 * 60) {
      int s = second % 60;
      int m = second ~/ 60;
      return ((m < 10) ? '0$m' : m).toString() + ':' + ((s < 10) ? '0$s' : s).toString();
    } else {
      int h = second ~/ (60 * 60);
      int m = second % (60 * 60) ~/ 60;
      int s = second % (60 * 60) % 60;

      return ((h < 10) ? '0$h' : h).toString() +
          ':' +
          ((m < 10) ? '0$m' : m).toString() +
          ':' +
          ((s < 10) ? '0$s' : s).toString();
    }
  }
}
